package services;

import domain.entities.Droga;
import domain.entities.Protocolo;

/**
 * Created with IntelliJ IDEA.
 * User: Guillermo
 * Date: 09/08/12
 * Time: 16:20
 * To change this template use File | Settings | File Templates.
 */
public class CriteriaProtocol extends Criteria{

    private Protocolo protocolo;

    public CriteriaProtocol(Protocolo protocolo){
        this.protocolo = protocolo;
    }

    @Override
    public boolean isIn(Object element) {
        Protocolo other = (Protocolo) element;

        boolean isIn = true;

        isIn &= isSubstring(protocolo.getId(), other.getId());
        isIn &= protocolo.getTherapeuticArea() == null || protocolo.getTherapeuticArea().getId() == null ||
                ( other.getTherapeuticArea() != null && other.getTherapeuticArea().getId() != null &&
                protocolo.getTherapeuticArea().getId().equals(other.getTherapeuticArea().getId()) );
        isIn &= protocolo.getSpeciality() == null || protocolo.getSpeciality().getId() == null ||
                ( other.getSpeciality() != null && other.getSpeciality().getId() != null &&
                        protocolo.getSpeciality().getId().equals(other.getSpeciality().getId()) );
        if(protocolo.getDrogas() != null && !protocolo.getDrogas().isEmpty()){
            boolean atLeastOne = false;
            for(Droga d : other.getDrogas()){
                if(isSubstring(protocolo.getDrogas().iterator().next().getDrugName(), d.getDrugName()) &&
                   isSubstring(protocolo.getDrogas().iterator().next().getDrugNumber(), d.getDrugNumber()))
                    atLeastOne = true;
            }
            isIn &= atLeastOne;
        }

        return isIn;
    }
}
